package com.demo;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.aivin.httpserver.R;
import com.demo.bean.EvenBusBean;
import com.demo.server.WkHttpServer;
import com.demo.tool.NetTool;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class HttpServerActivity extends Activity {
    private Context context ;
    private TextView progressShow , otherInfoShow ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ftp);

        context = this ;
        TextView textView = findViewById(R.id.textView);
        findViewById(R.id.closeFtpServer).setOnClickListener(onClickListener);

        progressShow = findViewById(R.id.progressShow) ;
        otherInfoShow = findViewById(R.id.otherInfoShow) ;


        String ip= NetTool.getIpAddressString() ;
        textView.setText(String.format( "http://%s:8080 \n 请手动授予权限，否则无法使用。" ,ip ));

        Intent intent = new Intent(context , WkHttpServer.class) ;
        context.startService(intent) ;

        requestPermission() ;
        EventBus.getDefault().register(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getProgressEventBus(EvenBusBean busBean){

        if(busBean.otherInfo!=null){
            otherInfoShow.setText(busBean.otherInfo);
        }

        if(busBean.progress!=null){
            progressShow.setText(busBean.progress);
        }

    }

    private void requestPermission() {
        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                .permission( Manifest.permission.READ_EXTERNAL_STORAGE )
                .permission(  Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {

                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if (quick) {
                            showToastMsg(   "拒绝授权将导致APP异常");
                            XXPermissions.startPermissionActivity(context, denied);
                        }
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(context ,WkHttpServer.class) ;
        context.stopService(intent) ;
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
             Toast.makeText(this ,"请用按钮退出" , Toast.LENGTH_SHORT).show();
            return  true ;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.closeFtpServer) {
                ((Activity) context).finish();
            }
        }
    };


    private void showToastMsg(String msg){
        Toast.makeText(this ,msg ,Toast.LENGTH_SHORT).show();
    }


}
